﻿using System.Net.NetworkInformation;
using System.Net.Sockets;

namespace Koala.Pro.Helpers
{
    public static class HardwareInfoHelper
    {
        public static List<string> GetMacAddress(string separator = "-")
        {
            IPGlobalProperties iPGlobalProperties = IPGlobalProperties.GetIPGlobalProperties();
            _ = iPGlobalProperties.HostName;
            _ = iPGlobalProperties.DomainName;
            NetworkInterface[] allNetworkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
            List<string> list = new List<string>();
            if (allNetworkInterfaces == null || allNetworkInterfaces.Length < 1)
            {
                return list;
            }
            foreach (NetworkInterface item in allNetworkInterfaces.Where((NetworkInterface c) => c.NetworkInterfaceType != NetworkInterfaceType.Loopback && c.OperationalStatus == OperationalStatus.Up))
            {
                if (!item.GetIPProperties().UnicastAddresses.Any((UnicastIPAddressInformation temp) => temp.Address.AddressFamily == AddressFamily.InterNetwork))
                {
                    continue;
                }
                PhysicalAddress physicalAddress = item.GetPhysicalAddress();
                if (string.IsNullOrEmpty(separator))
                {
                    list.Add(physicalAddress.ToString());
                    continue;
                }
                string text = "";
                byte[] addressBytes = physicalAddress.GetAddressBytes();
                for (int i = 0; i < addressBytes.Length; i++)
                {
                    text += addressBytes[i].ToString("X2");
                    if (i != addressBytes.Length - 1)
                    {
                        text += separator;
                    }
                }
                list.Add(text);
            }
            return list;
        }
    }
}
